<template>
  <div v-loading="loading" class="node">
    <div class="header">
      <div class="icon" />
      <div class="title">节点id</div>
    </div>
    <div class="body">
      <el-table
        v-loading="loading"
        :data="dataList"
      >
        <el-table-column
          prop="nodeId"
          label="节点id"
          width="660"
          show-overflow-tooltip
        >
          <div
            slot-scope="{row}"
            v-clipboard:copy="row.nodeId ? row.nodeId : ''"
            v-clipboard:error="onError"
            v-clipboard:success="onCopy"
            class="textOverflow"
          >
            {{ row.nodeId }}
          </div>
        </el-table-column>
        <el-table-column
          type="index"
          prop="size"
          label="节点类型"
          width="150"
        >
          <div slot-scope="{row}">{{ formatType(row.nodeId) }}</div>
        </el-table-column>
        <el-table-column
          prop="blockNumber"
          label="区块高度"
          width="150"
        />
        <el-table-column
          prop="pbftView"
          label="pbftView"
          width="150"
        >
          <div slot-scope="{row}">{{ row.pbftView || '--' }}</div>
        </el-table-column>
        <el-table-column
          prop="nodeActive"
          label="状态"
          width="150"
        >
          <div slot-scope="{row}">
            <div :class="row.nodeActive === 1 ? 'sucess_point' : 'fail_point'" />{{ statusFormat[row.nodeActive] }}
          </div>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { getNodeList, getConsensusNodeId } from '@/api/node'
export default {
  name: 'Node',
  components: { },
  props: {
    canShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      nodeTypeList: {
        sealer: '共识',
        observer: '观察',
        remove: '游离'
      },
      statusFormat: {
        1: '运行',
        2: '异常'
      },
      dataList: [],
      status_list: []
    }
  },
  watch: {
    canShow(n, o) {
      if (n) {
        this.loadData()
      }
    }
  },
  mounted() {
    if (this.canShow) {
      this.loadData()
    }
  },
  methods: {
    async loadData() {
      this.loading = true
      try {
        const res = await getConsensusNodeId({ groupId: 1, pageNumber: 1, pageSize: 100 })
        this.status_list = res.data
        const { code, data } = await getNodeList({ groupId: 1, pageNumber: 1, pageSize: 100 })
        if (!code) {
          this.dataList = data
        }
        this.loading = false
      } catch (error) {
        console.error(error)
      } finally {
        this.loading = false
      }
    },
    formatType(key) {
      if (!key) return
      for (let i = 0; i < this.status_list.length; i++) {
        const element = this.status_list[i]
        if (key === element.nodeId) {
          return this.nodeTypeList[element.nodeType]
        }
      }
    },
    onCopy() {
      this.$messageOnce.success('复制成功')
    },
    onError() {
      this.$messageOnce.error('复制失败')
    }
  }
}
</script>

<style lang="scss" scoped>
.node {
	width: 1320px;
	height: 324px;
	margin: 15px;
	padding: 0 20px;
	background: #FFFFFF;
	box-shadow: 0px 6px 25px 0px rgba(0, 0, 0, 0.04);
	border-radius: 10px;
	display: inline-block;
	.header {
		height: 64px;
		display: flex;
    align-items: center;
    .title {
      font-size: 18px;
      font-family: SourceHanSansCN-Medium, SourceHanSansCN;
      font-weight: 500;
      color: #2C2C2C;
    }
    .icon {
      width: 24px;
      height: 22px;
      background: url('../../assets/overview/node.png');
      background-size: 100%;
      margin-right: 20px;
    }
	}
  .body {
    padding: 0 10px;
  }
}

.textOverflow {
  overflow:hidden;
  text-overflow:ellipsis;
  white-space:nowrap;
}

.sucess_point{
  @include points;
  display: inline-block;
  background: #52C41A;
}

.fail_point{
  @include points;
	display: inline-block;
  background: #999999;
}
</style>
